package com.cslg.ac.demo.controller.file;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.file.FileService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:file.properties"},encoding="utf-8")
public class FileController {
    @Autowired
    FileService fileService;

    @Value("${imageLength}")
    int imageLength;
    @Value("${pictureLength}")
    int pictureLength;
    @Value("${textLength}")
    int textLength;
    @Value("${videoLength}")
    int videoLength;
    @Value("${petType}")
    int petType;
    @Value("${albumSize}")
    int albumSize;

    @RequestMapping(value = "/album/download",method = RequestMethod.POST)
    public void downloadAlbum(@RequestParam int id, @RequestParam int userType, @RequestParam int albumType,
                                PrintWriter out, HttpSession session){
        List<String> url;
        try {
            url = fileService.downloadAlbum(id,userType,albumType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("url",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }


    @RequestMapping(value = "/album/upload",method = RequestMethod.POST)
    public void uploadAlbum(@RequestParam MultipartFile[] album, @RequestParam int userType, @RequestParam int id, @RequestParam int albumType,
                            PrintWriter out){

        if(album == null || album.length > albumSize){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
            code = fileService.uploadAlbum(album,id,userType,albumType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);

    }





    @RequestMapping(value = "/video/download", method = RequestMethod.POST)
    public void downloadVideo(@RequestParam int id,@RequestParam int userType,@RequestParam int videoType,PrintWriter out){
        String url;
        try {
            url = fileService.downloadVideo(id,userType,videoType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("url",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }

    @RequestMapping(value = "/video/upload", method = RequestMethod.POST)
    public void uploadVideo(@RequestParam int id,@RequestParam int userType,@RequestParam int videoType,@RequestParam MultipartFile video,
                            PrintWriter out,HttpSession session){
        User user = (User)session.getAttribute("user");
        if(userType != petType){
            id = user.getId();
        }
        if(video.isEmpty() || video.getSize() > videoLength){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
            code = fileService.uploadVideo(id,userType,videoType,video);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);

    }

    @RequestMapping(value = "/photo/download", method = RequestMethod.POST)
    public void downloadPhoto(@RequestParam int id,@RequestParam int userType,@RequestParam int photoType,PrintWriter out){
        String url;
        try {
            url = fileService.downloadPhoto(id,userType,photoType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("photo",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/photo/upload",method = RequestMethod.POST)
    public void uploadPhoto(@RequestParam MultipartFile photo,@RequestParam int id,@RequestParam int userType,@RequestParam int photoType,
                            PrintWriter out,HttpSession session){
        User user = (User)session.getAttribute("user");
        if(userType != petType){
            id = user.getId();
        }
        if(photo.isEmpty() || photo.getSize() > imageLength){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;

        }
        int code;
        try {
            code = fileService.uploadPhoto(photo,id,userType,photoType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }


    @RequestMapping(value = "/text/download")
    public void downloadText(@RequestParam int id,@RequestParam int userType,@RequestParam int textType,PrintWriter out ){
        String url;
        try {
            url = fileService.downloadText(id,userType,textType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>(1);
        param.put("text",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/text/upload", method = RequestMethod.POST)
    public void uploadText(@RequestParam MultipartFile text,@RequestParam int userType,@RequestParam int id,@RequestParam int textType,
                           PrintWriter out,HttpSession session){
        User user = (User)session.getAttribute("user");
        if(userType != petType){
            id = user.getId();
        }
        if(text.isEmpty() || text.getSize() > textLength){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
            code = fileService.uploadText(text,id,userType,textType);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }


    @RequestMapping(value = "/image/download")
    public void downloadImage(@RequestParam int type,@RequestParam int id,PrintWriter out){
        String url;
        try {
            url = fileService.downloadImage(id,type);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("image",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }


    /**
     *
     * @param type
     * @param image
     * @param out
     */
    @RequestMapping(value = "/image/upload",method = RequestMethod.POST)
    public void uploadImage( @RequestParam int type, @RequestParam MultipartFile image,@RequestParam int id,
                             PrintWriter out, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(type != petType){
            id = user.getId();
        }
        if(image.isEmpty() || image.getSize() > imageLength){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
           code = fileService.uploadImage(image,id,type);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }
}
